home *** CD-ROM | disk | FTP | other *** search
/ Java Internet Programming Reference Guide / Java Internet Programming Reference Guide.iso / autorun / java.dir / 00102_3.txt < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.5 KB  |  193 lines

  1. JavaScript Values, Names, and Literals
  2.  
  3.  
  4.  
  5. ΓÇóValues ΓÇóVariable Names ΓÇóLiterals 
  6.  
  7.  
  8. ------------------------------------------------------------------------
  9.  
  10.  
  11. Values
  12.  
  13.  
  14.  
  15. JavaScript recognizes the following types of values: 
  16.  
  17. ΓÇónumbers, such as 42 or 3.14159 ΓÇólogical (Boolean) values, either true or false ΓÇóstrings, such as "Howdy!" ΓÇónull, a special keyword denoting a null value 
  18.  
  19.  
  20.  
  21. This relatively small set of types of values, or data types, enables you to perform useful functions with your applications. Notice that there is no explicit distinction between integer and real-valued numbers. Nor is there an explicit date data type. However, the date object and related built-in functions enable you to handle dates. 
  22.  
  23. Objects and functions are the other fundamental elements in the language. You can think of objects as named containers for values, and functions as procedures that your application can perform. 
  24.  
  25. Datatype Conversion
  26.  
  27.  
  28.  
  29. JavaScript is a loosely typed language. That means that you do not have to specify the datatype of a variable when you declare it, and datatypes are converted automatically as needed during the course of script execution. 
  30.  
  31. JavaScript will attempt to convert an expression to the datatype of the left-hand operand. Expressions are always evaluated from left to right, so JavaScript applies this rule at each step in the evaluation of a complex expression. 
  32.  
  33. For example, suppose you define the following variables 
  34.  
  35.  
  36. var astring = "7"
  37. var anumber = 42
  38.  
  39.  
  40.  
  41.  
  42.  
  43. Then consider the following statements: 
  44.  
  45.  
  46. x = astring + anumber
  47. y = anumber + astring
  48.  
  49.  
  50.  
  51.  
  52.  
  53. The first statement will convert anumber to a string value, because the left-hand operand, astring, is a string. The statement will then concatenate the two strings, so x will have a value of "742". 
  54.  
  55. Conversely, the second statement will convert astring to a numeric value, because the left-hand operand, anumber, is a number. The statement then adds the two numbers, so y will have a value of 49. 
  56.  
  57. JavaScript cannot convert some strings to numbers. For example, the statements 
  58.  
  59.  
  60. var anumber = 42
  61. var astring = "Phil"
  62. y = anumber + astring
  63.  
  64.  
  65.  
  66. will generate an error, becuase "Phil" cannot be converted to a number. 
  67.  
  68. The following table summarizes conversion between data types. 
  69.  
  70. NOTE: Much of the functionality specified in this table is not implemented as of Navigator beta4. 
  71.  
  72.  
  73.     Converted to data type: 
  74. Data type     function     object     number     Boolean     string 
  75. function     -     function     error     error     decompile     
  76. object
  77. Null object 
  78. error
  79. funobj OK 
  80. -     error
  81. true
  82. false 
  83. toString
  84. "null" 
  85. number (non-zero)
  86. 0
  87. Error (NaN)
  88. +infinity
  89. -infinity
  90. error     Number
  91. null
  92. Number
  93. Number
  94. Number 
  95. -     true
  96. false
  97. false
  98. true
  99. true
  100.  
  101. toString
  102. "0"BR>"NaN"BR>"Infinity""BR>"-Infinity""
  103. Boolean: false
  104. true 
  105. error     Boolean     0
  106. -     "false"BR>"true"
  107. string (non-null)
  108. null string 
  109. funstr OK
  110. error 
  111. String     numstr OK 
  112. error 
  113. true
  114. false 
  115.  
  116.  
  117.  
  118.  
  119. ------------------------------------------------------------------------
  120.  
  121.  
  122. Variable Names
  123.  
  124.  
  125.  
  126. You use variables to hold values in your application. You give these variables names by which you reference them, and there are certain rules to which the names must conform. 
  127.  
  128. A JavaScript identifier or name must start with a letter or underscore ("_"); subsequent characters can also be digits (0-9). Letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). JavaScript is case-sensitive. 
  129.  
  130. Some examples of legal names are: 
  131.  
  132. ΓÇóNumber_hits ΓÇótemp99 ΓÇó_name 
  133.  
  134.  
  135. ------------------------------------------------------------------------
  136.  
  137.  
  138. Literals
  139.  
  140.  
  141.  
  142. Literals are the way you represent values in JavaScript. These are fixed values that you literally provide in your application source, and are not variables. Examples of literals include: 
  143.  
  144. ΓÇó42 ΓÇó3.14159 ΓÇó"To be or not to be" 
  145.  
  146.  
  147.  
  148. Integers
  149.  
  150.  
  151.  
  152. Integers can be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8) format. A decimal integer literal consists of a sequence of digits (optionally suffixed as described below) without a leading 0 (zero). 
  153.  
  154. An integer can be expressed in octal or hexadecimal rather than decimal. A leading 0 (zero) on an integer literal means it is in octal; a leading 0x (or 0X) means hexadecimal. Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7. 
  155.  
  156. Floating Point Literals
  157.  
  158.  
  159.  
  160. A floating point literal can have the following parts: a decimal integer, a decimal point ("."), a fraction (another decimal number), an exponent, and a type suffix. The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by a "+" or "-"). A floating point literal must have at least one digit, plus either a decimal point or "e" (or "E"). Some examples of floating point literals are: 
  161.  
  162. ΓÇó3.1415 ΓÇó-3.1E12 ΓÇó.1e12 ΓÇó2E-12 
  163.  
  164.  
  165.  
  166. Boolean Literals
  167.  
  168.  
  169.  
  170. The boolean type has two literal values: true and false. 
  171.  
  172. String Literals
  173.  
  174.  
  175.  
  176. A string literal is zero or more characters enclosed in double (") or single (') quotes. A string must be delimited by quotes of the same type; that is, either both single quotes or double quotes. The following are examples of string literals: 
  177.  
  178. ΓÇó"blah" ΓÇó'blah' ΓÇó"1234" ΓÇó"one line \n another line" 
  179.  
  180.  
  181.  
  182. Special Characters
  183.  
  184.  
  185.  
  186. You can use the following special characters in JavaScript string literals: 
  187.  
  188. ΓÇó\b indicates a backspace. ΓÇó\f indicates a a form feed. ΓÇó\n indicates a new line character. ΓÇó\r indicates a carriage return. ΓÇó\t indicates a tab character. 
  189.  
  190.